﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRGBCP(DataSet db)
        {
            int x = 4, y = 1;
            string page = "";
            DataTable dtOutput = new DataTable();
            try
            {  
                #region======Title===========
                exAgent.SetCellValue(3, 1, "仓库"); dtOutput.Columns.Add("LO", typeof(System.String));
                exAgent.SetCellValue(3, 2, "  "); dtOutput.Columns.Add("LB", typeof(System.String));
                exAgent.SetCellValue(3, 3, "日期"); dtOutput.Columns.Add("MDATE", typeof(System.String));
                exAgent.SetCellValue(3, 4, "品种"); dtOutput.Columns.Add("ART", typeof(System.String));
                exAgent.SetCellValue(3, 5, "色号"); dtOutput.Columns.Add("FARBE", typeof(System.String));
                exAgent.SetCellValue(3, 6, "纺批"); dtOutput.Columns.Add("SPPART", typeof(System.String));
                exAgent.SetCellValue(3, 7, "捻批"); dtOutput.Columns.Add("ZWPART", typeof(System.String));
                exAgent.SetCellValue(3, 8, "纱型"); dtOutput.Columns.Add("FBPART", typeof(System.String));
                exAgent.SetCellValue(3, 9, "包装"); dtOutput.Columns.Add("BEM1", typeof(System.String));
                exAgent.SetCellValue(3, 10, "昨日库存"); dtOutput.Columns.Add("BS00", typeof(System.String));
                exAgent.SetCellValue(3, 11, "库存"); dtOutput.Columns.Add("STOCKQTY", typeof(System.String));
                exAgent.SetCellValue(3, 12, "采购"); dtOutput.Columns.Add("BS11", typeof(System.String));
                exAgent.SetCellValue(3, 13, "生产"); dtOutput.Columns.Add("BS13", typeof(System.String));
                exAgent.SetCellValue(3, 14, "发货"); dtOutput.Columns.Add("BS20", typeof(System.String));
                exAgent.SetCellValue(3, 15, "接收"); dtOutput.Columns.Add("BS21", typeof(System.String));
                exAgent.SetCellValue(3, 16, "手工入库"); dtOutput.Columns.Add("BS43", typeof(System.String));

                #endregion

                #region======ConvertData===
                #endregion
                var qGW = from GW in db.Tables["ICC_STOCK"].AsEnumerable()
                          group GW by new
                          {
                              LB = GW["LB"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                foreach (var GW in qGW)
                {
                    page = GW.KEY.LB;
                    exAgent.SetCurrentSheet("YGRGBCP");
                    exAgent.CopySheet(page); 
                    DataRow[] rows = db.Tables["ICC_STOCK"].Select("LB='"+page+"'");
                    DataTable dtSoruce = rows.CopyToDataTable();
                    dtOutput.Clear();
                    dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                  
                    #region====Excel==========
                    int count = dtOutput.Rows.Count;
                    exAgent.RangeCopy(4, 50, 5, 4 + count);
                    exAgent.DataTableToExcel(dtOutput, x, 1);

                    //MergeSame(x, count);
                    exAgent.SetCellValue(1, 1, 1);
                    exAgent.BatchConvert2Number(4, 10, count + 3, 16); 
                    exAgent.SetCellValue(1, 1, " ");
                    #endregion 
                }
                exAgent.SetCurrentSheet("YGRGBCP"); 
                dtOutput.Clear();
                dtOutput.Merge(db.Tables["ICC_STOCK"], false, MissingSchemaAction.Ignore);
                int countx = dtOutput.Rows.Count;
                exAgent.DataTableToExcel(dtOutput, x, 1);
                exAgent.BatchConvert2Number(4, 10, countx + 3, 16);
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                exAgent.RenameSheet(date);
                exAgent.SetCurrentSheet(0);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

    }
}